﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
using System.Text;
using System.Web.Routing;
namespace Htss.Platform.Web
{
    public  class ControllerBox
    {
        private ViewDataDictionary _viewData;
        private ViewDataDictionary _verificationMessage;
        private Dictionary<string, Data.DbTable> _viewDbTable;
        public ControllerBox() 
        {
            
        }
        /// <summary>
        /// 存储指定的NameSpace,BoxName,ActionName
        /// </summary>
        public Dictionary<string,string> ActionBox
        {
            get;
            set;
        }
        public RouteData RouteData
        {
            get { return RequestContext.RouteData; }
        }
        public Dictionary<string, Data.DbTable> ViewDbTable
        {
            get {
                if (_viewDbTable == null) {
                    _viewDbTable = new Dictionary<string, Data.DbTable>();
                }
                return _viewDbTable;
            }
            set {
                _viewDbTable = value;
            }
        }
        public ViewDataDictionary ViewData
        {
            get{
                if (_viewData == null){
                    _viewData = new ViewDataDictionary();
                }
                return _viewData;
            }
            set {
                _viewData = value;
            }
        }
        public ViewDataDictionary VerificationMessage
        {
            get { 
                if(_verificationMessage==null){
                    _verificationMessage = new ViewDataDictionary();
                }
                return _verificationMessage;
            }
            set
            {
                _verificationMessage = value;
            }
        }
        public RequestContext RequestContext
        {
            get;
            set;
        }
        public HttpContextBase HttpContext
        {
            get { return RequestContext.HttpContext; }
        }
        public HttpRequestBase Request
        {
            get { return HttpContext.Request; }
        }

        public HttpResponseBase Response
        {
            get { return HttpContext.Response; }
        }

        public HttpSessionStateBase Session
        {
            get { return HttpContext.Session; }
        }

        public HttpServerUtilityBase Server
        {
            get { return HttpContext.Server; }
        }
        /// <summary>
        /// 初始化默认值
        /// </summary>
        /// <param name="RequestContext"></param>
        public void Init(RequestContext RequestContext,Dictionary<string,string> ActionBox)
        {
            this.RequestContext = RequestContext;
            this.ActionBox = ActionBox;
            HtmlHelper = new HtmlHelper();
        }

        public void SetModel(Data.BasicModel Model)
        {
            ModelAssignment.SetModel(Request, Model);
        }
        public ActionResult View()
        {
            return View(null);
        }
        private HtmlHelper HtmlHelper
        {
            get;
            set;
        }
        public ActionResult View(string viewClass)
        {
            //辅助类赋值
            
            HtmlHelper.ViewData = ViewData;
            HtmlHelper.ViewDbTable = ViewDbTable;
            HtmlHelper.VerificationMessage = VerificationMessage;
            HtmlHelper.RequestContext = RequestContext;
            HtmlHelper.Request = Request;
            HtmlHelper.Response = Response;
            HtmlHelper.Session = Session;

            //
            ViewResult result = new ViewResult(viewClass);
            result.HtmlHelper = HtmlHelper;
            result.RequestNamespace = ActionBox["NameSpace"];
            result.RequestBoxName = ActionBox["BoxName"];
            result.ExecuteResult(HttpContext);
            return result;
        }
    }
}
